<?php

/**
 * Yabsta News
 *
 * This file is a subject under proprietary sofrware license
 * 
 * @category
 * @package
 * @subpackage
 * @author      valerii lazarevych <lazarevuch@gmail.com>
 * @copyright   Copyright(c) Valerii Lazarevych
 * @license     proprietary software license
 * @version     $Id: AjaxController.php 26.05.2012 20:46:48 valerii $
 */

/**
 * Class AjaxController
 * 
 * Application container for all ajax functions.
 */
class AjaxController extends Zend_Controller_Action
{
    /**
     * Sphinx search engine
     * @var SphinxClient
     */
    private  static $_searchEngine   =   null;
    
    /**
     * Get search engine object 
     * to make search actions
     * 
     * @return search engine obj
     */
    public static function getSearchEngine()
    {
        if (!self::$_searchEngine)  {
            
            require_once  'Zend/sphinx_api/sphinxapi.php';            
            $cl  =   new SphinxClient ();
            
            $cl->SetConnectTimeout(1);        
            $cl->SetWeights     ( array('title' => 20, 'tags' => 15) );
            $cl->SetMatchMode   (SPH_MATCH_EXTENDED2); 
            $cl->SetSortMode    (SPH_SORT_RELEVANCE);
            //$cl->SetLimits      (0, 20, 1000);            
            $cl->SetRankingMode(SPH_RANK_PROXIMITY_BM25);
                        
            self::$_searchEngine    =   $cl;       }
            
        return self::$_searchEngine;
    }

    /**
     * Initialize method
     * Do things
     * 1. Turn off Zend layout rendering
     */
    public function init()
    {
        if ($this->getRequest()->isXmlHttpRequest()){            
            $this->_helper->layout->disableLayout();    }
    }
    
    /**
     * Tags filtring action
     * 
     * Filtering news list by tags
     */
    public function newsfiltringAction()
    {   
        $newModel   =   new models_News;        
        echo    Zend_Json::encode( $newModel->newsFiltringByTags( explode(' ', $this->_getParam('tags_filter')) ));
    }
    
    /**
     * News search action
     * 
     * News searching method 
     */
    public function newssearchingAction()
    {
        $newModel   =   new models_News;
        echo    Zend_Json::encode( $newModel->newsLooking( explode(' ',$this->_getParam('news_search_str')) ));        
    }
    
    /**
     * Method which is gonna be helper 
     * and will be working with sphinx to  make search
     */
    public function sphinxSearch($query_str, $field = 'title')
    {                                         
        $output     =   self::getSearchEngine()->Query( '@'.$field.' '.$query_str, 'newsIndex');                
        $idsList    =   array_keys((array)$output['matches']);
        
        $newsObj    =   new domainObjects_News();
                        
        foreach ( (array)$idsList as $newsId){
            
            $newsObj->setId($newsId);
            $newsObj->find();             
            $newsList[]   =    array( 'id'    => $newsObj->getId(),
                                      'title' => $newsObj->newsTitle(),
                                      'tags'  => $newsObj->newsTags()  );     }     
                                      
        return isset($newsList) ? $newsList :  array();
    }       
}
